package java基础知识;

import java.io.*;

/**
 * @author coderFu
 * @create 2022/2/13
 */
public class TransientDemo {

    public static void toolFunction1(){

        try {
            //序列化Resume
            Resume resume = new Resume();
            resume.setName("fyl");
            resume.setAge("23");
            Resume.setSex("男");

            resume.display();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("resume.txt"));
            oos.writeObject(resume);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void toolFunction2(){

        try {
            //反序列化
            Resume resume = new Resume();

            Resume.setSex("女");

            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("resume.txt"));
            Object o = ois.readObject();
            resume = (Resume)o;
            resume.display();
            ois.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        toolFunction1();
        toolFunction2();
    }







}


class Resume implements Cloneable, Serializable{
    private static final long serialVersionUID = -2327046010382156683L;

    private String name;

    private static  String sex;

    transient private String age;

    /**
     * transient 修饰符在 集合框架中应用较多，用来记录集合的修改次数。
     * @return
     */

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static String getSex() {
        return sex;
    }

    public static void setSex(String sex) {
        Resume.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public Object clone(){
        try {
            return (Resume) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void display(){
        System.out.println("名字是："+this.name+"  性别是:"+sex+"  年龄是："+this.age);
    }
}
